using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Info.Basic;
using EntLibEC.Biz;
using EntLibEC.Biz.Basic;

namespace EntLibEC.View.Basic
{
	/// <summary>
	/// Summary description for ShipTypePayTypeUnSearch.
	/// </summary>
	public partial class ShipTypePayTypeUnSearch : PageBase
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			btnAdd.Attributes["onclick"]= "javascript:openWindowS3('ShipTypePayTypeOpt.aspx?opt=add'); return false;";

			if( !Page.IsPostBack )
			{
                this.ucPayType.BindPayType(true, true);
                this.ucPayType.PayTypeSysNo = AppConst.IntNull;
                this.ucShipType.BindShipType(true, true);
                this.ucShipType.ShipTypeSysNo = AppConst.IntNull;

                BindGrid();
			}
		}

		private void BindGrid()
		{
			DataTable dt = BuildTable();
			DataGrid1.DataSource = dt;
			DataGrid1.DataBind();

			int rows = 0;
			if ( Util.HasMoreRow(dt) )
				rows = dt.Rows.Count;
			Assert(lblMessage, rows.ToString() + " record(s)", 1);
		}

		private DataTable BuildTable()
		{
			DataTable table = new DataTable("ShipPay");
			table.Columns.Add(new DataColumn("SysNo",typeof(int)));
			table.Columns.Add(new DataColumn("ShipTypeName",typeof(string)));
			table.Columns.Add(new DataColumn("PayTypeName",typeof(string)));
			

			Hashtable unHt;
            if (ucShipType.ShipTypeSysNo == AppConst.IntNull
                && ucPayType.PayTypeSysNo == AppConst.IntNull)
            {
                unHt = ASPManager.GetInstance().GetShipPayHash();
            }
            else
            {
                unHt = ASPManager.GetInstance().GetShipPayHash(ucShipType.ShipTypeSysNo, ucPayType.PayTypeSysNo);
            }

			Hashtable shipHt = ASPManager.GetInstance().GetShipTypeHash();
            Hashtable payHt = ASPManager.GetInstance().GetPayTypeHash();

			foreach(ShipPayInfo item in unHt.Values)
			{
				DataRow row = table.NewRow();
				row["SysNo"] = item.SysNo;
				row["ShipTypeName"] = ((ShipTypeInfo)shipHt[item.ShipTypeSysNo]).ShipTypeName;
				row["PayTypeName"] = ((PayTypeInfo)payHt[item.PayTypeSysNo]).PayTypeName;

				table.Rows.Add(row);
			}
			return table;

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);

		}
		#endregion

		private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.AreaShipPay, false, lblMessage) )
				return;
			if ( e.CommandName == "delete" )
			{
				int shipPaySysNo = (int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
				try
				{
					ASPManager.GetInstance().DeleteShipPayUn(shipPaySysNo);
					LogManager.GetInstance().Write(new LogInfo(shipPaySysNo, (int)AppEnum.LogType.Basic_ShipPayUn_Delete, base.sInfo));
					Assert(lblMessage, "Remove the ShipType ! PayType ok", 1);
				}
				catch(BizException exp)
				{
					Assert(lblMessage, exp.Message, -1);
				}
				catch(Exception exp)
				{
					Assert(lblMessage, "Remove the ShipType ! PayType failed, see log", -1);
					ErrorLog.GetInstance().Write(exp.ToString());
				}
				BindGrid();
			}
		}

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}
	}
}
